package quick.portal.activity;

import quick.common.AppContext;
import quick.common.AppManager;
import quick.common.UIHelper;
import quick.portal.R;
import quick.portal.fragment.email.EmailMainFragment;
import quick.portal.fragment.home.HomeMainFragment;
import quick.portal.fragment.notice.NoticeFragment;
import quick.portal.fragment.workorder.WorkOrderFragment;
import quick.portal.service.AppInfoService;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends BaseFragmentActivity {
	
	private RadioGroup group;
	
    private FragmentManager fragmentManager;
    
    protected ImageButton btnMenu;
	protected PopupMenu menu;
	
	public PopupMenu getPopupMenu()
	{
		this.menu.getMenu().clear();
		this.menu.getMenu().add(R.id.menu_group_system,R.id.menu_exit_app,1,"安全退出");
    	this.menu.getMenu().add(R.id.menu_group_system,R.id.menu_modify_password,2,"修改密码");
    	this.menu.getMenu().add(R.id.menu_group_system,R.id.menu_update_app,3,"更新版本");
    	
		return this.menu;
	}
	
	public boolean menuItemClick(MenuItem item) {
		if(item.getItemId() == R.id.menu_exit_app)
		{
			this.logout();
            return true;
		}
		
		if(item.getItemId() == R.id.menu_modify_password)
		{
			UIHelper.showActivity(this, ModifyPasswordActivity.class);
            return true;
		}
		
		if(item.getItemId() == R.id.menu_update_app)
		{
			this.updateApp();
            return true;
		}
		
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String username = AppContext.getInstance().getUserName();
		if(username==null)
		{
			UIHelper.showActivity(this, LoginActivity.class);
			return;
		}
		
		this.setContentView(R.layout.main_activity);
		this.fragmentManager = getSupportFragmentManager();
		
		TextView tvHeadTitle = (TextView)this.findViewById(R.id.textHeadTitle);
		tvHeadTitle.setText("欢迎您," + username);
		
		this.initMenuButton();
		
		this.initFootBar();
		
		Intent service = new Intent(this,MonitingService.class);
		this.startService(service);
	}

	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
	
	private void initFootBar() {
		
        group = (RadioGroup) findViewById(R.id.group);
        
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            	
            	Fragment fragment = null;
            	
                switch (checkedId) {
                    case R.id.foot_bar_home:
                    	fragment = new HomeMainFragment();
                    	break;
                    case R.id.foot_bar_notice:
                    	fragment = new NoticeFragment();
                    	break;
                    case R.id.foot_bar_workorder:
                    	fragment = new WorkOrderFragment();
                    	break;
                    case R.id.main_footbar_email:
                    	fragment = new EmailMainFragment();
                    	break;
                }
                
                addFragmentToStack(fragment);
            }
        });
        
        addFragmentToStack(new HomeMainFragment());
    }
	
	private void addFragmentToStack(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        
        transaction.replace(R.id.fragment_container,fragment);
        transaction.addToBackStack(null);
        
        transaction.commit();
    }
	
	private void initMenuButton()
	{
		this.btnMenu = (ImageButton)this.findViewById(R.id.btnMenu);
        this.btnMenu.setVisibility(View.VISIBLE);
        
        this.menu = new PopupMenu(this, this.btnMenu);

		this.btnMenu.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                menu.show();
            }
        });
	}
	
	private void logout()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		builder.setTitle(R.string.app_name);
		builder.setMessage("确定要退出本次登陆吗?");
		
		builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				AppManager.getAppManager().finishAllActivity();				
				AppContext.getInstance().clearUserInfo();	        	
	        	UIHelper.showActivity(MainActivity.this, LoginActivity.class);

			}
		});
		
		builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		
		builder.create().show();
	}
	
	private void updateApp()
	{
		new AppInfoService().updateApp();
	}
	
}
